package org.example.xml;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.example.pojo.BOM;
import org.example.pojo.Member;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class Dom4jExample {

    /**
     * 读取xml文件，并转换为pojo对象
     * 此方法可以实例化动态域的值列、BOM到XOM列
     */
    public static Map<String,BOM> xmlToPojo(String xmlPath) {
        Map<String,BOM> bomMap = new HashMap<>();
        try {
            //创建SAXReader对象,读取xml文件
            SAXReader reader = new SAXReader();
            Document document = reader.read(new File(xmlPath));
            //获取根元素
            Element root = document.getRootElement();
            System.out.println("根元素：" + root.getName());
            //获取所有class元素
            List<Element> classes = root.elements("class");
            for (Element element : classes) {
                BOM bom = new BOM();
                bom.setClassName(element.elementText("businessName").trim());
                Map<String,Member> memberList = new HashMap<>();
                //获取所有attribute元素
                List<Element> xmlMemberList = element.elements("attribute");
                for (Element attribute : xmlMemberList) {
                    Member member = new Member();
                    member.setCode(attribute.elementText("name").trim());
                    member.setXOMValue(attribute.elementText("getter").trim());
                    bom.putMembersMap(member);
                }
                bomMap.put(bom.getClassName(),bom);
            }
        } catch (DocumentException e) {
            System.out.println("解析xml文件异常,错误信息是"+e.getMessage());
            e.printStackTrace();
        }
        return bomMap;
    }

    public static void main(String[] args) {

        try {
            //创建SAXReader对象,读取xml文件
            SAXReader reader = new SAXReader();
            Document document = reader.read(new File("D:\\01MyFile\\赵政\\NBBOM\\bom\\动态域.b2x"));

            //获取根元素
            Element root = document.getRootElement();
            System.out.println("根元素：" + root.getName());
            List<BOM> bomList = new ArrayList<>();
            List<Element> classes = root.elements("class");
            for (Element element : classes) {
                BOM bom = new BOM();
                bom.setClassName(element.elementText("businessName").trim());
                List<Member> memberList = new ArrayList<>();
                List<Element> xmlMemberList = element.elements("attribute");
                for (Element attribute : xmlMemberList) {
                    Member member = new Member();
                    member.setCode(attribute.elementText("name").trim());
                    member.setXOMValue(attribute.elementText("getter").trim());
                    bom.addMembers(member);
                }

                bomList.add(bom);
            }
            for (int i = 0; i < 10; i++) {
                System.out.println(bomList.get(i));
            }
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        }
    }
}
